home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / MPW C ƒ / SonOfGrepƒ / Grep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-20  |  5.0 KB  |  308 lines  |  [TEXT/MPS ]

  1. /* GREP.C  a generalized regualr expression parser. */
  2.  
  3. #include    <stdio.h>
  4. /* MJS: include "argsim.h" not needed */
  5. #include    "tools.h"
  6. #include    "Grep-MJS.h"
  7. #include <CursorCtl.h>    /* MJS: added for rotating cursor */
  8.  
  9. #define    MAXLINE        128
  10. #define    MAX_EXPR    64
  11. #define Rotation    2    /* MJS: added for rotating cursor */
  12.  
  13. int    vflag, yflag, cflag, lflag, nflag, hflag, fflag ;
  14.  
  15. /* MJS: add a global so argv[0] can be used in error messages */
  16. char * FullToolName;
  17.  
  18. main(argc, argv)
  19. int    argc ;
  20. char    **argv ;
  21. {
  22.     int    i, j, linenum, count ;
  23.     int    line[MAXLINE] ;
  24.     int    numfiles ;
  25.     FILE    *stream ;
  26.     int    exprc ;
  27.     TOKEN    *exprv[MAX_EXPR] ;
  28.  
  29.     /* MJS: first thing, load tool name global */
  30.     FullToolName = argv[0];
  31.         
  32. #ifdef    DEBUG
  33.     fprintf(stderr, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") ;
  34. #endif
  35.     i = 1 ;
  36.     if(argc<2)
  37.         abort(pr_usage(1)) ;
  38.     if(*argv[i] == '-')
  39.     {
  40.         expand_sw(argv[i++]) ;
  41.         if(i == argc)
  42.             abort(pr_usage(1)) ;
  43.     }
  44.     if((exprc = get_expr(exprv, MAX_EXPR, &argv[i++])) == 0)
  45.         abort (pr_usage(2)) ;
  46.     numfiles = argc - 1 ;
  47.     do
  48.     {
  49.         if(numfiles)
  50.         {
  51.             stream = fopen(argv[i], "r") ;
  52.             if(stream == NULL)
  53.             {
  54.                 fprintf(stderr, "Can't open %s\n", argv[i]) ;
  55.                 getchar() ;
  56.                 continue ;
  57.             }
  58.         }
  59.         else
  60.         {
  61.             stream = stdin ;
  62.         }
  63.         count = 0 ;
  64.         linenum = 1 ;
  65.         while (fgets(line, MAXLINE, stream))
  66.         {
  67. #ifdef CPM
  68.             if(!fflag || yflag)
  69.                 stoupper(line) ;
  70. #else
  71.             if(yflag)
  72.                 stoupper(line) ;
  73. #endif
  74.             for(j=exprc; --j >= 0; )
  75.             {
  76.                 if(matchs(line, exprv[j]))
  77.                 {
  78.                     count++ ;
  79.                     pr_match(linenum, line, argv[i], 1, numfiles) ;
  80.                 }
  81.                 else
  82.                 {
  83.                     pr_match(linenum, line, argv[i], 0, numfiles) ;
  84.                 }
  85.                 linenum++ ;
  86.                 cntrl_c() ;
  87.             }
  88.             /* MJS: this is a good place to rotate cursor */
  89.             SpinCursor(Rotation);
  90.             if(lflag && count)
  91.                 break ;
  92.         }
  93.         pr_count(numfiles, argv[i], count) ;
  94.         fclose(stream) ;
  95.     } while(++i < argc) ;
  96.     OSreset() ;
  97.     abort() ;
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. pr_count (fcount, fname, count)
  105. int    fcount, count ;
  106. char    *fname ;
  107. {
  108. #ifdef    DEBUG
  109.     fprintf(stderr, "pr_count entered.\n") ;
  110. #endif
  111.     if(!cflag)
  112.         return ;
  113.     if(fcount > 1)
  114.         printf("%-12s: ", fname) ;
  115.     printf("%d\n", count) ;
  116. }
  117.  
  118.  
  119.  
  120.  
  121. pr_match (linenum, line, fname, match, numfiles)
  122. int    linenum, match, numfiles ;
  123. char    *line, *fname ;
  124. {
  125.     char    buf[80] ;
  126. #ifdef    DEBUG
  127.     fprintf(stderr, "pr_match entered.\n") ;
  128. #endif
  129.     if(cflag)
  130.         return ;
  131.     if((vflag && !match) || (!vflag && match))
  132.     {
  133.         /* MJS: Modified output to be more MPW-useful */
  134.         if(!hflag && ((numfiles > 1) || lflag))
  135.             printf("\tFile \042%s\042%s", fname, lflag ? "\n" : ";") ;
  136.         if(nflag)
  137.             printf("\tLine %03d;", linenum) ;
  138.         if(!lflag)
  139.             printf("\t# %s", line) ;
  140.     }
  141. }
  142.  
  143.  
  144.  
  145.  
  146. pr_usage (num)
  147. int    num ;
  148. {
  149. #ifdef DEBUG
  150.     fprintf(stderr, "%d ", num) ;
  151. #endif
  152.     /* MJS: use argv[0] instead of "grep" */
  153.     fprintf(stderr, "usage: %s [-cefhlnvy] [expression] ,<files...>\n", ToolName()) ;
  154. }
  155.  
  156.  
  157.  
  158.  
  159. abort ()
  160. {
  161.     /* MJS: Indicate completion, exit without error */
  162.     printf("# %s done\n", ToolName());
  163.     exit(0) ;
  164. }
  165.  
  166.  
  167.  
  168.  
  169. expand_sw (str)
  170. char    *str ;
  171. {
  172.     vflag = 0;
  173.     cflag = 0 ;
  174.     lflag = 0 ;
  175.     nflag = 0 ;
  176.     hflag = 0 ;
  177.     fflag = 0 ;
  178.     yflag = 0 ;
  179. #ifdef    DEBUG
  180.     fprintf(stderr, "expand_sw entered.\n") ;
  181. #endif
  182.     while(*str)
  183.     {
  184.         switch(toupper(*str))
  185.         {
  186.         case '-':
  187.         case 'E':    break ;
  188.         case 'C':    cflag = 1 ;    break ;
  189.         case 'F':    fflag = 1 ;    break ;
  190.         case 'H':    hflag = 1 ;    break ;
  191.         case 'L':    lflag = 1 ;    break ;
  192.         case 'N':    nflag = 1 ;    break ;
  193.         case 'V':    vflag = 1 ;     break ;
  194.         case 'Y':    yflag = 1 ;    break ;
  195.         defalut:    pr_usage(3) ;
  196.                 abort() ;
  197.                 break ;
  198.         }
  199.         str++ ;
  200.     }
  201. }
  202.  
  203.  
  204.  
  205.  
  206. int do_or (lp, expr, max)
  207. char    *lp ;
  208. TOKEN    **expr ;
  209. int    max ;
  210. {
  211.     int    found ;
  212.     TOKEN    *pat ;
  213.     char    *op ;
  214. #ifdef    DEBUG
  215.     fprintf(stderr, "do_or entered.\n") ;
  216. #endif
  217.     
  218.     found = 0 ;
  219.     if(yflag)
  220.         stoupper(lp) ;
  221.     while (op = in_string(OR_SYM, lp))
  222.     {
  223.         if(found <= max && (pat = makepat(lp, OR_SYM)))
  224.         {
  225.             *expr++ = pat ;
  226.             found++ ;
  227.         }
  228.         lp = ++op ;
  229.         if(pat == 0)
  230.             goto fatal_err ;
  231.     }
  232.     if(found <= max && (pat = makepat(lp, OR_SYM)))
  233.     {
  234.         found++ ;
  235.         *expr = pat ;
  236.     }
  237.     if(pat == 0)
  238.     {
  239.     fatal_err:
  240.             printf("Illegal expression\n") ;
  241.             exit() ;
  242.     }
  243.     return(found) ;
  244. }
  245.  
  246.  
  247.  
  248.  
  249. get_expr (expr, max, defexpr)
  250. TOKEN    *expr[] ;
  251. int    max ;
  252. char    **defexpr ;
  253. {
  254.     FILE    *stream ;
  255.     int    count ;
  256.     char    line[MAXLINE] ;
  257. #ifdef DEBUG
  258.     int i ;
  259.     fprintf(stderr, "get_expr entered.\n") ;
  260. #endif
  261.     count = 0 ;
  262.     if(fflag)
  263.     {
  264. #ifdef    DEBUG
  265.         fprintf(stderr, "fflag true in get_expr\n") ;
  266. #endif
  267.         if((stream = fopen(*defexpr, "r")) == NULL)
  268.         {
  269.             fprintf(stderr, "Can't open %s\n", *defexpr) ;
  270.             abort() ;
  271.         }
  272.         while((max - count) && fgets(line, MAXLINE, stream))
  273.         {
  274.             count += do_or(line, &expr[count], max - count) ;
  275. #ifdef    DEBUG
  276.             fprintf(stderr, "Count = %d\n", count) ;
  277. #endif
  278.         }
  279.         fclose(stream) ;
  280.     }
  281.     else
  282.     {
  283.         if(count += do_or(*defexpr, &expr[count], max - count))
  284.             *defexpr = " " ;
  285.     }
  286. #ifdef DEBUG
  287.     for(i=count; --i >= 0; )
  288.     {
  289.         pr_tok(expr[i]) ;
  290.         printf("----------------------------------------------\n") ;
  291.     }
  292. #endif
  293.     return(count) ;
  294. }
  295.  
  296.  
  297.  
  298.  
  299. cntrl_c()
  300. {
  301. #ifdef CPM
  302.     if(bdos(11) && ((bdos(1,0) & ox7f) == 0x03))
  303.         abort() ;
  304. #endif
  305. #ifdef    DEBUG
  306.     fprintf(stderr, "cntrl_c entered.\n") ;
  307. #endif
  308. }